/**
 * Created by Neville Kadwa.
 */

package org.shiftone.cdep.graph;

import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

import org.apache.log4j.Logger;
import org.shiftone.cdep.store.StoreManager;
import org.shiftone.cdep.store.StoreException;
import org.shiftone.cdep.store.StoreManagerFactory;
import com.touchgraph.graphlayout.TGException;
import com.touchgraph.graphlayout.Node;
import com.touchgraph.graphlayout.Edge;
import com.touchgraph.graphlayout.GLPanel;

public class Graph extends GLPanel
{

    public static final Logger LOG = Logger.getLogger(Graph.class);

    private HashMap classMap = null;

    public Graph()
    {
        super();
        localityScroll.setLocalityRadius(1);
    }

    private Node getClassNode(Integer classId, String className) throws TGException
    {
        if (classMap == null)
            classMap = new HashMap();

        String key = "Class " + classId;
        String label = className;

        Node node = (Node) classMap.get(key);

        if (node == null)
        {
            LOG.debug("creating node: " + key);
            node = tgPanel.addNode(label);
            classMap.put(key, node);
        }

        return node;
    }

    private void setUp(StoreManager storeManager, Map classEntry, Node sourceNode, Node targetNode) throws StoreException
    {

        Integer sourceId = (Integer) classEntry.get("SOURCE_CLASS_ID");
        Integer targetId = (Integer) classEntry.get("TARGET_CLASS_ID");

        LOG.debug("checking class " + sourceId);
        Map sourceClass = storeManager.queryClass(sourceId.intValue());
        Boolean isInterface = (Boolean) sourceClass.get("INTERFACE_IN");
        if (isInterface != null && isInterface.booleanValue())
        {
            sourceNode.setType(Node.TYPE_ROUNDRECT);
        }

        LOG.debug("checking class " + targetId);
        Map targetClass = storeManager.queryClass(targetId.intValue());

        isInterface = (Boolean) targetClass.get("INTERFACE_IN");
        if (isInterface != null && isInterface.booleanValue())
        {
            targetNode.setType(Node.TYPE_ROUNDRECT);
        }
    }

    public void randomGraph() throws TGException
    {
        try
        {
            StoreManager storeManager = StoreManagerFactory.getStoreManager();

            LOG.debug("RandomGraph: " + storeManager);
            List classDepList = storeManager.queryClassDependencies();

            Iterator iter = classDepList.iterator();
            while (iter.hasNext())
            {
                Map classEntry = (Map) iter.next();

                Node sourceNode = getClassNode((Integer) classEntry.get("SOURCE_CLASS_ID"),
                                               (String) classEntry.get("SOURCE_NAME_TX"));

                Node targetNode = getClassNode((Integer) classEntry.get("TARGET_CLASS_ID"),
                                               (String) classEntry.get("TARGET_NAME_TX"));

                setUp(storeManager, classEntry, sourceNode, targetNode);

                tgPanel.addEdge(sourceNode, targetNode, Edge.DEFAULT_LENGTH);
            }
        }
        catch (StoreException sex)
        {
            sex.printStackTrace();
            throw new TGException(sex);
        }
    }

    public static void main(String[] args)
    {

        try
        {

        JFrame frame;
        frame = new JFrame("Graph Layout");
        Graph glPanel = new Graph();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        frame.getContentPane().add("Center", glPanel);
        frame.setSize(500,500);
        frame.setVisible(true);
        }
        catch (Exception ex)
        {
            LOG.error("main", ex);
            System.exit(1);
        }

    }
}

/* EOF */
